<?php

namespace app\home\controller;

use app\common\controller\Home;
use think\Request;

class Ranking extends Home
{
    public function __construct()
    {
        parent::__construct();

        //加载模型
        $this->BusinessModel = model('Business.Business');
        $this->SubjectModel = model('Subject.Subject');
        $this->OrderModel = model('Subject.Order');
    }    


    //榜单列表
    public function index()
    {   
        if($this->request->isAjax())
        {
            $page = $this->request->param('page',1,'trim');
            $limit = $this->request->param('limit',10,'trim');
            $keywords = $this->request->param('keywords','','trim');

            //偏移量
            $start = ($page-1)*$limit;


            $where = [];

            if(!empty($keywords))
            {
                $where['title'] = ['LIKE',"%$keywords%"];
            }

            //查询数据总条数
            $count = $this  ->SubjectModel
                            ->with(['teacher','category'])
                            ->where($where)
                            ->count();
            //查询数据
            $list = $this->SubjectModel
                         ->with(['teacher','category'])
                         ->where($where)
                         ->order('likes','desc')
                         ->limit($start,$limit)
                         ->select();

            $data = [
                'count' => $count,
                'list'  => $list
            ];

            if($list)
            {
                //成功返回
                $this->success('返回列表',null,$data);
                exit;
            }else
            {
                //失败返回
                $this->error('暂无数据',null,$data);
                exit;
            }
        }

        return $this->view->fetch();
    }
}
